<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog"
      :custom-class="'panna-dialog_medium'"
      :title="dataList.UserRoleAssociation"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
    >
      <el-form ref="formData" :model="formData" :rules="formDataRules" status-icon>
        <el-form-item
          :label="dataList.UserId"
          :label-width="formLabelWidth"
          prop="userId"
          :error="errorMsg.userId"
        >
          <el-input v-model="formData.userId" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item
          :label="dataList.RoleId"
          :label-width="formLabelWidth"
          prop="roleId"
          :error="errorMsg.roleId"
        >
          <el-input v-model="formData.roleId" autocomplete="off" type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button :disabled="loading" type="primary" @click="onSubmit('formData')">{{ btn.Confirm }}</el-button>
        <el-button @click="cancel">{{ btn.Cancel }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { create, update } from "../../../api/Permission/PermissionUserRole.js";

export default {
  name: "PermissionUserRoleEdit",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      innerDialogFormVisible: false,
      formLabelWidth: "100px",
      initData: {
        id: 0,
        userId: 0,
        roleId: 0,
      },
      formData: {
        id: 0,
        userId: 0,
        roleId: 0,
      },
      formDataRules: {
        userId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        roleId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
      },
      initErrorMsg: {
        userId: "",
        roleId: "",
      },
      errorMsg: {
        userId: "",
        roleId: "",
      },
    };
  },
  computed: {
    btn() {
      return this.$t("Permission.buttons");
    },
    dataList() {
      return this.$t("Permission.PermissionUser");
    },
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  methods: {
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0) {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg("userId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("roleId", this.errorMsg, res.msg);
      }
    },
  },
};
</script>
